package com.library.uiframework.mvp;

import java.util.LinkedList;

/**
 * Created by jiangtao on 16/6/1.
 *
 * @author jiangtao
 * @version 1.0.0
 */
public class PageLifecycleHelper implements IPageLifecycle {

    private LinkedList<IPageLifecycle> mPageLifecycles = new LinkedList<>();

    public void register(IPageLifecycle lifecycle) {
        if (lifecycle == null) {
            return;
        }
        if (mPageLifecycles.contains(lifecycle)) {
            return;
        }
        mPageLifecycles.add(lifecycle);
    }

    private void clear() {
        mPageLifecycles.clear();
    }

    @Override
    public void onCreate() {
        for (IPageLifecycle lifecycle : mPageLifecycles) {
            lifecycle.onCreate();
        }
    }

    @Override
    public void onStart() {
        for (IPageLifecycle lifecycle : mPageLifecycles) {
            lifecycle.onStart();
        }
    }

    @Override
    public void onResume() {
        for (IPageLifecycle lifecycle : mPageLifecycles) {
            lifecycle.onResume();
        }
    }

    @Override
    public void onPause() {
        for (IPageLifecycle lifecycle : mPageLifecycles) {
            lifecycle.onPause();
        }
    }

    @Override
    public void onStop() {
        for (IPageLifecycle lifecycle : mPageLifecycles) {
            lifecycle.onStop();
        }
    }

    @Override
    public void onDestroy() {
        for (IPageLifecycle lifecycle : mPageLifecycles) {
            lifecycle.onDestroy();
        }
        clear();
    }
}
